//
//  GSTabBarViewController.m
//  GSTabbarDemo
//
//  Created by 陈JJ on 2018/11/21.
//  Copyright © 2018年 陈JJ. All rights reserved.
//

#import "GSTabBarViewController.h"
#import "ViewController.h"

#define UIImageMake(img) [UIImage imageNamed:img inBundle:nil compatibleWithTraitCollection:nil]

@interface GSTabBarViewController ()<UINavigationControllerDelegate,UITabBarControllerDelegate>

@end

@implementation GSTabBarViewController

#pragma mark - instance

- (instancetype)init {
    if (self = [super init]) {
        ViewController *homeVc = [[ViewController alloc] init];
        homeVc.view.backgroundColor = [UIColor redColor];
        UINavigationController *homeNaviVc = [[UINavigationController alloc] initWithRootViewController:homeVc];
        homeNaviVc.delegate = self;
        homeNaviVc.tabBarItem = [self tabBarItemWithTitle:@"首页" image:[UIImageMake(@"icon_home_N") imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] selectedImage:[UIImageMake(@"icon_home_S") imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] tag:0];
        
        ViewController *nearbyVc = [[ViewController alloc] init];
        nearbyVc.view.backgroundColor = [UIColor greenColor];
        UINavigationController *nearbyNaviVc = [[UINavigationController alloc] initWithRootViewController:nearbyVc];
        nearbyNaviVc.delegate = self;
        nearbyNaviVc.tabBarItem = [self tabBarItemWithTitle:@"附近" image:[UIImageMake(@"icon_nearBy_N") imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] selectedImage:[UIImageMake(@"icon_nearBy_S") imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] tag:1];
        
    
        ViewController *N2NVc = [[ViewController alloc] init];
        N2NVc.view.backgroundColor = [UIColor yellowColor];
        UINavigationController *N2NNaviVc = [[UINavigationController alloc] initWithRootViewController:N2NVc];
        N2NNaviVc.delegate = self;
        N2NNaviVc.tabBarItem = [self tabBarItemWithTitle:@"N2N" image:[UIImageMake(@"N2Nicon") imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] selectedImage:[UIImageMake(@"N2Nicon") imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] tag:2];
        
        ViewController *msgVc = [[ViewController alloc] init];
        msgVc.view.backgroundColor = [UIColor blueColor];
        UINavigationController *msgNaviVc = [[UINavigationController alloc] initWithRootViewController:msgVc];
        msgNaviVc.delegate = self;
        msgNaviVc.tabBarItem = [self tabBarItemWithTitle:@"消息" image:[UIImageMake(@"icon_msg_N") imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] selectedImage:[UIImageMake(@"icon_msg_S") imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] tag:3];
        
        ViewController *mineVc = [[ViewController alloc] init];
        mineVc.view.backgroundColor = [UIColor cyanColor];
        UINavigationController *mineNaviVc = [[UINavigationController alloc] initWithRootViewController:mineVc];
        mineNaviVc.delegate = self;
        mineNaviVc.tabBarItem = [self tabBarItemWithTitle:@"我的" image:[UIImageMake(@"icon_mine_N") imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] selectedImage:[UIImageMake(@"icon_mine_S") imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] tag:4];
        
        self.viewControllers = @[homeNaviVc, nearbyNaviVc, N2NNaviVc, msgNaviVc, mineNaviVc];
    }
    
    self.delegate = self;
    return self;
}

#pragma mark - system callback

- (void)viewWillLayoutSubviews {
    [super viewWillLayoutSubviews];
    
    UITabBarItem *item = self.tabBar.items[2];
    item.imageInsets = UIEdgeInsetsMake(-25, 0, 0, 0);
    
}


- (void)viewDidLayoutSubviews {
    [super viewDidLayoutSubviews];
}


- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
}

#pragma mark - private method

- (void)itemMiddleTap {
    self.selectedIndex = 2;
}

- (UITabBarItem *)tabBarItemWithTitle:(NSString *)title image:(UIImage *)image selectedImage:(UIImage *)selectedImage tag:(NSInteger)tag {
    UITabBarItem *tabBarItem = [[UITabBarItem alloc] initWithTitle:title image:image tag:tag];
    tabBarItem.selectedImage = selectedImage;
    return tabBarItem;
}

#pragma mark - UINavigationControllerDelegate

- (nullable id <UIViewControllerAnimatedTransitioning>)navigationController:(UINavigationController *)navigationController
                                            animationControllerForOperation:(UINavigationControllerOperation)operation
                                                         fromViewController:(UIViewController *)fromVC
                                                           toViewController:(UIViewController *)toVC {
    if (UINavigationControllerOperationPush == operation) {
        toVC.hidesBottomBarWhenPushed = (navigationController.viewControllers.count >= 0);
    } else {
        fromVC.hidesBottomBarWhenPushed = (navigationController.viewControllers.count >= 1);
    }
    
    return nil;
}

#pragma mark - UITabBarControllerDelegate

- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController {
    UITabBarItem *item = tabBarController.tabBar.items[2];
    UIImageView *itemImageView  = [item gs_imageView];
    if (itemImageView && !itemImageView.isUserInteractionEnabled) {
        itemImageView.userInteractionEnabled = YES;
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(itemMiddleTap)];
        [itemImageView addGestureRecognizer:tap];
    }
    
    NSLog(@"%p -- isUserInteractionEnabled -> %@",&itemImageView,itemImageView.isUserInteractionEnabled ? @"YES" : @"NO");
    return YES;
}


@end
